'use strict';
module.exports = {
	/**
	 * 赠送用户课程
	 * @url admin/system/user/sys/giveUserCourse 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, item, total_fee, user_id, comment, pay_type } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		let order_id = await vk.baseDao.add({
			dbName: "uni-id-base-order",
			dataJson: {
				title: item.title,
				type: "course",
				user_id: user_id,
				status: 2,
				create_time: new Date().getTime(),
				order_no: vk.pubfn.createOrderNo(),
				paid_time: new Date().getTime(),
				total_fee,
				comment,
				pay_type,
				success_year: new Date().getFullYear(),
				success_month: new Date().getMonth() + 1,
				success_day: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd"),
				success_week: new Date().getDay()
			}
		})

		let course = item
		await vk.baseDao.add({
			dbName: "order_goods",
			dataJson: {
				'oid': order_id,
				'user_id': user_id,
				'goods_id': course['_id'],
				'charge': course['charge'],
				'type': "course",
				'num': 1,
				'goods_name': "[点播]" + course['title'],
				'goods_thumb': course['thumb'],
				'goods_charge': course['charge'],
				'goods_ori_charge': course['charge']
			}
		})

		let time = 360
		if (item.validity_period) time = item.validity_period
		
		await vk.baseDao.add({
			dbName: "user_goods",
			dataJson: {
				goods_id: item._id,
				goods_type: "course",
				oid: order_id,
				admin_send: true,
				user_id,
				end_time: new Date().getTime() + time * 24 * 60 * 60 * 1000
			}
		})

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
